﻿using CTP.CRM.Core;
using CTP.CRM.Core.SharedModel.Pub;
using Flurl.Http;
using Microsoft.AspNetCore.Http;

namespace CTP.CRM.Application.FlurlRequest
{
    /// <summary>
    /// Flur的http请求
    /// </summary>
    public class FlurlRequestService : IFlurlRequestService
    {
        /// <summary>
        /// 接口默认时间：如果配置文件读取的时间为0，设置默认值：20秒
        /// </summary>
        public const int INTERFACE_TIMEOUT = 20 * 1000;

        //private readonly IInternalUserService _internalUserService;

        /////<inheritdoc/>
        //public FlurlRequestService(IInternalUserService internalUserService)
        //{
        //    _internalUserService = internalUserService;
        //}

        /// <summary>
        /// 请求第三方http请求
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="timeOutMilliseconds">超时时间：单位为毫秒</param>
        /// <returns></returns>
        /// <exception cref="BusinessException"></exception>
        public IFlurlRequest RequestBase(string url, int timeOutMilliseconds = 3000)
        {
            timeOutMilliseconds = timeOutMilliseconds == 0 ? INTERFACE_TIMEOUT : timeOutMilliseconds;
            return url.WithTimeout(TimeSpan.FromMilliseconds(timeOutMilliseconds));
        }

        /// <summary>
        /// 请求第三方http请求:携带Header
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="timeOutMilliseconds">超时时间：单位为毫秒</param>
        /// <returns></returns>
        /// <exception cref="BusinessException"></exception>
        public IFlurlRequest RequestHeader(string url, int timeOutMilliseconds = 3000)
        {
            timeOutMilliseconds = timeOutMilliseconds == 0 ? INTERFACE_TIMEOUT : timeOutMilliseconds;
            IFlurlRequest flurlRequest = null;
            var headerDictionary = VerfiyHeader();
            if (headerDictionary != null)
            {
                flurlRequest = url.WithHeader("SystemRequest", headerDictionary["SystemRequest"])
                    .WithHeader("Authorization", headerDictionary["Authorization"])
                    .WithHeader("SubSystemId", headerDictionary["SubSystemId"])
                    //.WithHeader("Accept-Language", headerDictionary["Accept-Language"])  //添加语言
                    .WithTimeout(TimeSpan.FromMilliseconds(timeOutMilliseconds));
            }
            else
            {
                //Authorization
                string strAuthorization = TokenGeneratorHelper.GeneratorSystemToken();
                // AuthenticationHeaderValue authenticationHeaderValue = new AuthenticationHeaderValue("Bearer", strAuthorization.ReplaceFirst("Bearer", "").TrimStart());
                //SubSystemId
              //  int currentSystemId = _internalUserService.CurrentSystemId;

                flurlRequest = url
                    .WithHeader("Authorization", strAuthorization)
                   // .WithHeader("SubSystemId", currentSystemId)
                    // .WithHeader("Accept-Language", headerDictionary["Accept-Language"])  //添加语言
                    .WithTimeout(TimeSpan.FromMilliseconds(timeOutMilliseconds));
            }

            return flurlRequest;
        }

        /// <summary>
        /// 等到Header数据
        /// </summary>
        /// <returns></returns>
        private IHeaderDictionary VerfiyHeader()
        {
            return CTPABPCore.HttpContext?.Request?.Headers ?? null;
        }
    }
}
